package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.Model;
import org.dromara.dao.business.domain.ModelQuery;
import org.dromara.dao.business.mapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class ModelService {

    @Autowired
    ModelMapper modelMapper;


    public TableDataInfo<Model> queryPageList(ModelQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                modelMapper.paginationSelectList(query, pageQuery),
                modelMapper.paginationSelectCount(query)
        );
    }


    public Model queryById(Long id) {
        return modelMapper.selectById(id);
    }


    @Transactional
    public Model insert(Model entity) {
        modelMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(Model update) {

        Model entity = modelMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setName(update.getName());
        entity.setValue(update.getValue());
        entity.setIcon(update.getIcon());
        entity.setDescription(update.getDescription());
        entity.setManu(update.getManu());
        entity.setDiamondFee(update.getDiamondFee());
        entity.setStarFee(update.getStarFee());
        entity.setStatus(update.getStatus());
        entity.setUpdateTime(LocalDateTime.now());
        entity.setEnableNetwork(update.getEnableNetwork());
        entity.setType(update.getType());
        entity.setLevel(update.getLevel());
        entity.setImageScale(update.getImageScale());
        modelMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        modelMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        modelMapper.deleteByIds(Arrays.asList(ids));
    }




    public List<Model> getValidModelList(String type) {
        return modelMapper.selectList(new LambdaQueryWrapper<Model>().eq(Model::getStatus, true).eq(Model::getType, type));
    }


    public List<Model> getAllModelList() {
        return modelMapper.selectList(new LambdaQueryWrapper<>());
    }
}
